package com.cloud.wms.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.text.method.ScrollingMovementMethod;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.cloud.core.widget.dialog.CommonDialog;
import com.cloud.wms.R;

import java.text.MessageFormat;

import static android.view.KeyEvent.KEYCODE_BACK;
import static android.view.View.GONE;
import static android.view.View.VISIBLE;

/**
 * @Author： Chen
 * @Date： 2021/5/18
 * @Desc：
 */
public class UpdateDialog extends Dialog implements View.OnClickListener {
    private TextView tv_update_title;

    private TextView tv_update_log;

    private TextView btn_update;

    private RelativeLayout fl_progress;

    private ProgressBar pb_progress;

    private TextView tv_progress_percent;

    private String updateInfo;
    private String updateTitle;

    public interface DownloadListener {
        void beginDownload();

        void cancelClicked();
    }

    private DownloadListener mListener;

    public UpdateDialog(@NonNull Context context, String updateInfo, DownloadListener downloadListener) {
        this(context, R.style.DialogStyle);
        this.updateInfo = updateInfo;
        this.mListener = downloadListener;
    }

    public UpdateDialog(@NonNull Context context, String updateTitle, String updateInfo, DownloadListener downloadListener) {
        this(context, R.style.DialogStyle);
        this.updateInfo = updateInfo;
        this.updateTitle = updateTitle;
        this.mListener = downloadListener;
    }

    public UpdateDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        View view = View.inflate(getContext(), R.layout.dialog_update_apk, null);
        setContentView(view);
        tv_update_title = view.findViewById(R.id.tv_update_title);
        if (!TextUtils.isEmpty(updateTitle)) {
            tv_update_title.setText(updateTitle);
        }
        tv_update_log = view.findViewById(R.id.tv_update_info);
        tv_update_log.setText(updateInfo);
        btn_update = view.findViewById(R.id.btn_update);
        fl_progress = view.findViewById(R.id.fl_progress);
        pb_progress = view.findViewById(R.id.pb_progress);
        tv_progress_percent = view.findViewById(R.id.tv_percent);

        tv_update_log.setMovementMethod(ScrollingMovementMethod.getInstance());
        btn_update.setOnClickListener(this);

        setOnKeyListener((dialog, keyCode, event) -> {
            if (keyCode == KEYCODE_BACK && mListener != null && event.getAction() == KeyEvent.ACTION_UP) {
                mListener.cancelClicked();
            }
            return false;
        });

        setCancelable(false);
        setCanceledOnTouchOutside(false);
        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        DisplayMetrics d = getContext().getResources().getDisplayMetrics();
        lp.width = (int) (d.widthPixels * 0.8);
        dialogWindow.setAttributes(lp);
    }

    @Override
    public void onClick(View v) {
        if (mListener != null) {
            mListener.beginDownload();
        }
    }

    /**
     * 设置进度百分比
     *
     * @param progress
     */
    public void setProgress(int progress) {
        pb_progress.setProgress(progress);
        tv_progress_percent.setText(MessageFormat.format("下载百分比:{0}%", progress / 100));
    }

    /**
     * 设置更新按钮文案
     *
     * @param updateText
     */
    public void setUpdateText(String updateText) {
        btn_update.setText(updateText);
    }


    /**
     * 切换dialog模式 下载中/非下载中
     *
     * @param isProgressing
     */
    public void switchViewState(boolean isProgressing) {
        if (isProgressing) {
            fl_progress.setVisibility(VISIBLE);
            btn_update.setVisibility(GONE);
        } else {
            fl_progress.setVisibility(GONE);
            btn_update.setVisibility(VISIBLE);
        }
    }
}
